<?php
/**
 * 
 * @author return_wdm
 *
 */
class AnnounceBehaviors
{
	/**
	 *创建公告
	 * @param $params=array(userid username subject content sort,link);
	 * @param unknown_type $params
	 */
	public function create($params)
	{
		try{
		if (empty($params['userid']))return false;
		if(empty($params['subject'])) return false;
		
		if(empty($params['content'])) return false;
		
		$announce=new Announce();
		
		$announce->from_user_id=isset($params['userid'])?$params['userid']:null;
		$announce->from_user_name=isset($params['username'])?$params['username']:null;
		$announce->subject=isset($params['subject'])?$params['subject']:null;
		$announce->content=isset($params['content'])?$params['content']:null;
		$announce->sort=isset($params['sort'])?$params['sort']:0;
		$announce->link=isset($params['link'])?$params['link']:null;
		$announce->save();
		return true;
		}catch(Exception $e)
		{
			return $e->getMessage();
		}
		
		
	}
	/**
	 * 删除对应id的公告
	 * @param unknown_type $announce_id
	 */
	public function delete($announce_id)
	{
		$tempannounce=Announce::model()->findByPk($announce_id);
		$tempannounce->delete();
		return 1;
	}
	/**
	 * 更新公告
	 * array(id,subject,content,sort,link)
	 * @param unknown_type $params
	 */
	public function update($params)
	{
		if(empty($params['id']))
			return false;
		
		$TempAnnounce=Announce::model()->findByPk($params['id']);
		if($TempAnnounce==null)
			return false;
		$TempAnnounce->subject=$params['subject'];
		$TempAnnounce->content=$params['content'];
		$TempAnnounce->update_time=time();
		$TempAnnounce->sort=$params['sort'];
		$TempAnnounce->link=$params['link'];
		$TempAnnounce->save();
			return true;
	}
	/**
	 * 以列表形式返回一系列公告信息
	 * @param $params//用于查询时候填充参数
	 * $params==null 普通方式返回列表
	 * @param unknown_type $page
	 * @param unknown_type $pagesize
	 * @return list=array(array(id,subject.....),array())
	 */
	public function getList($params=null,$currentpage=1,$pagesize=10)
	{
		
		
		$criteria=new CDbCriteria();
		if(!empty($params))
		{
			$keyword=$params['keyword'];
			$criteria->addSearchCondition('subject', $keyword,true,'OR');
			$criteria->addSearchCondition('content', $keyword,true,'OR');
		}
		$count=Announce::model()->count($criteria);
		if(($currentpage-1)*$pagesize>$count+1)
			return false;
			
		$page=new CPagination($count);
		$page->currentPage=$currentpage-1;
		
		$page->pageSize=$pagesize;
		
		$page->applyLimit($criteria);
		$criteria->order="sort DESC,id DESC";
		$list=Announce::model()->findAll($criteria);
		$result=array();
		foreach ($list as $key=>$value)
		{
			$temparray=array();
			$temparray['id']=$value['id'];
			$temparray['from_user_id']=$value['from_user_id'];
			$temparray['from_user_name']=$value['from_user_name'];
			$temparray['type']=$value['type'];
			$temparray['subject']=$value['subject'];
			$temparray['content']=$value['content'];
			$temparray['sort']=$value['sort'];
			$temparray['link']=$value['link'];
			$temparray['status']=$value['status'];
			$temparray['create_time']=$value['create_time'];
			$temparray['update_time']=$value['update_time'];
			$result[]=$temparray;
		}
		
		
		return $result;
		
	}
	/**
	 * 根据Sort排序后返回公告列表
	 * @param unknown_type $currentpage
	 * @param unknown_type $pagesize
	 * @return list=array(array(id,subject.....),array())
	 */
	public function getListbySort($currentpage=1,$pagesize=10)
	{
		
		$criteria=new CDbCriteria();
		$count=Announce::model()->count($criteria);
		
		
		if(($currentpage-1)*$pagesize>$count+1)
			return false;
		$page=new CPagination($count);
		$page->currentPage=$currentpage-1;
		$page->pageSize=$pagesize;
		$page->applyLimit($criteria);
		
		$criteria->order="sort DESC";
		
		$list=Announce::model()->findAll($criteria);
		$result=array();
		foreach ($list as $key=>$value)
		{
			$temparray=array();
			$temparray['id']=$value['id'];
			$temparray['from_user_id']=$value['from_user_id'];
			$temparray['from_user_name']=$value['from_user_name'];
			$temparray['type']=$value['type'];
			$temparray['subject']=$value['subject'];
			$temparray['content']=$value['content'];
			$temparray['sort']=$value['sort'];
			$temparray['link']=$value['link'];
			$temparray['status']=$value['status'];
			$temparray['create_time']=$value['create_time'];
			$temparray['update_time']=$value['update_time'];
			$result[]=$temparray;
		}
		
		return $result;
		
	}
	/**
	 * 获取公告id的公告信息
	 * @param unknown_type $id
	 * retrun array(id,subject,content,create_time....)
	 */
	public function getDetail($id)
	{
		$TempAnnounce=Announce::model()->findByPk($id);
		if($TempAnnounce==null)
			return false;
		$temparray=array();
		$temparray['id']=$TempAnnounce['id'];
		$temparray['from_user_id']=$TempAnnounce['from_user_id'];
		$temparray['from_user_name']=$TempAnnounce['from_user_name'];
		$temparray['type']=$TempAnnounce['type'];
		$temparray['subject']=$TempAnnounce['subject'];
		$temparray['content']=$TempAnnounce['content'];
		$temparray['sort']=$TempAnnounce['sort'];
		$temparray['link']=$TempAnnounce['link'];
		$temparray['status']=$TempAnnounce['status'];
		$temparray['create_time']=$TempAnnounce['create_time'];
		$temparray['update_time']=$TempAnnounce['update_time'];
		return $temparray;
	}
	/**
	 * 获取公告信息数量
	 * @param unknown_type $criteria
	 * return $count;
	 */
	public function getCount($criteria=null)
	{
		$Announce_count = Announce::model()->count($criteria) ;
		return $Announce_count ;
	}
	/**
	 * 
	 * 进行排序参数为
	 * array(array(id,sort),array(id,sort))
	 * @param unknown_type $params
	 * $return true;
	 */
	public function Sort($params)
	{
		foreach ($params as $key=>$value )
		{
			$data['sort']=$value['sort'];
			$b_db_complete = Announce::model()->updateByPk($value['id'],$data);
		}
		return true;
	}
	public static function changeInttoDate($int)
	{
		
	} 
	
	
}